package com.tyc.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * 通过标志位优雅的停止线程
 */
public class StopThread {
    private static volatile boolean stop = false;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            run();
        });
        thread.start();
        TimeUnit.SECONDS.sleep(1);
        stop = true;
        thread.join();
    }

    public static void run(){
        while (!stop){
            System.out.println("运行");
        }
        System.out.println("运行结束");
    }
}
